<template>
  <div>
    <el-dialog
    :modal="false"
      :title="toChild"
      :visible.sync="dialogFormVisible"
      :close-on-click-modal="false"
      width="25%"
      lock-scroll
      @close="closeDialog"
    >
      <div class="register_form_main">
        <el-row style="height: 100%">
          <el-col :span="24">
            <div class="grid-content-right">
              <el-form
                :model="userEditForm"
                :rules="userEditFormRules"
                ref="userEditRef"
                size="mini"
              >
                <el-form-item label="岗位名称 :" prop="name">
                  <el-input
                    type="text"
                    v-model="userEditForm.name"
                    placeholder="请填写岗位名称"
                    clearable
                  ></el-input>
                </el-form-item>
                <el-form-item label="岗位技能 :" prop="skill">
                  <el-input
                    type="text"
                    v-model="userEditForm.skill"
                    placeholder="请填写岗位技能"
                    clearable
                  ></el-input>
                </el-form-item>
                <el-form-item label="项目 :" prop="projectId">
                  <el-select
                    v-model="userEditForm.projectId"
                    placeholder="请选择接项目"
                    filterable
                    clearable
                    @change="queryson"
                  >
                    <el-option
                      v-for="item in projectData"
                      :key="item.index"
                      :label="item.name"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>

                <el-form-item label="客户 :" prop="customerName">
                  <el-input v-model="userEditForm.customerName" disabled>
                  </el-input>
                </el-form-item>
                <el-form-item label="岗位需求人数 :" prop="number">
                  <el-input
                    type="text"
                    v-model="userEditForm.number"
                    placeholder="岗位需求人数"
                  ></el-input>
                </el-form-item>
                <el-form-item label="计划满足日期 :" prop="date">
                  <el-col :span="11">
                    <el-date-picker
                      type="date"
                      placeholder="计划满足日期"
                      value-format="yyyy-MM-dd"
                      v-model="userEditForm.date"
                      style="width: 100%"
                      filterable
                      clearable
                    ></el-date-picker>
                  </el-col>
                </el-form-item>
                <el-form-item label="最晚到岗时间" prop="latestArrivalTime">
                  <el-col :span="11">
                    <el-date-picker
                      type="date"
                      placeholder="选择日期"
                      value-format="yyyy-MM-dd"
                      v-model="userEditForm.latestArrivalTime"
                      style="width: 100%"
                      filterable
                      clearable
                    ></el-date-picker>
                  </el-col>
                </el-form-item>
                <el-form-item label="办公地点 :" prop="address">
                  <el-select
                    v-model="userEditForm.address"
                    placeholder="请选择办公地点"
                    filterable
                    clearable
                  >
                    <el-option
                      v-for="item in regionData"
                      :key="item.index"
                      :label="item.name"
                      :value="item.id"
                    ></el-option>
                  </el-select>
                </el-form-item>
                <el-form-item label="岗位职责 :" prop="position">
                  <el-input
                    type="textarea"
                    v-model="userEditForm.position"
                    placeholder="请输入岗位职责..."
                    clearable
                  ></el-input>
                </el-form-item>
                <el-form-item label="岗位要求 :" prop="requirements">
                  <el-input
                    type="textarea"
                    v-model="userEditForm.requirements"
                    placeholder="请输入岗位要求..."
                    clearable
                    filterable
                  ></el-input>
                </el-form-item>
              </el-form>
            </div>
          </el-col>
        </el-row>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" size="mini" @click="onCertain"
            >保 存</el-button
          >
          <el-button
            class="cancel"
            type="info"
            @click="dialogClose"
            size="mini"
            >取 消</el-button
          >
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
//创建岗位/编辑岗位
import { establishPost, editPost } from "@/api/post";
import { queryProject } from "@/api/project";
export default {
  props: {
    toChild: String,
    customerData: "",
    regionData: "",
     projectData:''
  },
  data() {
    return {
      ProjectAll: [],
      dialogFormVisible: false,
      fileType: {
        fileType: 0,
      },
      nowIndex: -1,
      skill: [
        {
          ski: "JAVA",
        },
        {
          ski: "WEB",
        },
        {
          ski: "UI",
        },
      ],
      userEditForm: {
        address: "",
        customerName: "",
        date: "",
        detailAddress: "",
        latestArrivalTime: "",
        number: "",
        position: "",
        id: "",
        name: "",
        projectId: "",
        requirements: "",
        skill: "",
customerId:''
      },
      initFormData: {},
      // 正则验证
      userEditFormRules: {
        name: [
          {
            required: true,
            message: "请填写岗位名称",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^[^\s]*$/,
            message: "不支持空格格式",
            trigger: "blur",
          },
        ],
        position: [
          {
            required: true,
            message: "请填写岗位职责",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "首尾不能为空格",
            trigger: "blur",
          },
        ],
        skill: [
          {
            required: true,
            message: "请输入岗位技能",
            trigger: ["blur", "change"],
          },
        ],
        requirements: [
          {
            required: true,
            message: "请填写岗位需求",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "首尾不能为空格",
            trigger: "blur",
          },
        ],
        projectId: [
          {
            required: false,
            message: "请选择项目名称",
            trigger: ["blur", "change"],
          },
        ],
        customerName: [
          {
            required: false,
            message: "请填写客户名称",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "首尾不能为空格",
            trigger: "blur",
          },
        ],
        number: [
          {
            required: true,
            message: "请填写需求人数",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "首尾不能为空格",
            trigger: "blur",
          },
        ],
        date: [
          {
            required: true,
            message: "请选择完成时间",
            trigger: ["blur", "change"],
          },
        ],
        latestArrivalTime: [
          {
            required: true,
            message: "请选择最晚到岗时间",
            trigger: ["blur", "change"],
          },
        ],
        address: [
          {
            required: true,
            message: "请填写办公地点",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "首尾不能为空格",
            trigger: "blur",
          },
        ],
        detailAddress: [
          {
            required: true,
            message: "请填写详细地址",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "首尾不能为空格",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    //自动选择
    queryson(e) {
      if(e){
        this.projectData.forEach((item) => {
        if (item.id == e) {
          this.userEditForm.customerName = item.customerName;
          this.userEditForm.customerId = item.customerId;
        }
      });
      }else{
        this.userEditForm.customerName = '';
          this.userEditForm.customerId = '';
      }
    },
    // 弹窗
    openDialog(row) {
      this.dialogFormVisible = true; // 让弹窗显示
      if (row) {       
        this.userEditForm = JSON.parse(JSON.stringify( row));
      } 
    },
    // 取消
    dialogClose() {
      this.dialogFormVisible = false;
      this.$refs["userEditRef"].resetFields();
      this.userEditForm={}
    },
    /* 保存  */
    onCertain() {
      if (this.userEditForm.id) {
        this.$refs["userEditRef"].validate((valid) => {
          if (valid) {
            console.log(
              this.userEditForm,
              "----修改的-----",
              this.initFormData.id
            );
            editPost(this.userEditForm, this.userEditForm.id).then(
              (res) => {
                if (res && res.code && res.code === "00000") {
                  this.$message.success("修改成功！");
                  this.dialogClose();
                  // this.resetForm();
                  console.log("修改成功！");
                  this.$parent.queryPost();
                }
              }
            );
          } else {
            return false;
          }
        });
      } else {
        this.$refs["userEditRef"].validate((valid) => {
          if (valid) {
           
            establishPost(this.userEditForm).then((res) => {
              if (res && res.code && res.code === "00000") {
                this.$message.success("创建成功！");
                this.dialogClose();
                console.log("创建成功！");
                this.$parent.queryPost();
              }
            });
          } else {
            return false;
          }
        });
      }
    },
    
  },
};
</script>

<style lang="less" scoped>
.el-form {
  padding: 10px 50px 0;
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  .el-input-group__append {
    padding: 0 2px;
  }
}
.passwordat {
  display: none;
}
.el-form-item {
  display: flex;
}
::v-deep .el-form-item__label {
  width: 120px;
  text-align: left;
}
::v-deep .el-input__inner {
  width: 250px;
}
::v-deep .el-textarea__inner {
  min-height: 100px !important;
  width: 250px;
  font-size: 12px;
  font-family: "微软雅黑";
}
::v-deep .el-dialog {
  width: 30%;
}

.dialog-footer {
  padding: 2px 20px 20px;
}
::v-deep .el-dialog__body {
  padding: 0;
}
::v-deep .el-dialog {
  min-width: 400px;
}
</style>